package com.itheima.mm.service.impl;

import com.itheima.mm.mapper.DictMapper;
import com.itheima.mm.pojo.Dict;
import com.itheima.mm.service.DictService;
import com.itheima.mm.vo.DictVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description DictServiceImpl
 * @Author xiao
 * @Date 2024-06-26
 */
@Service
public class DictServiceImpl implements DictService {

    @Autowired
    private DictMapper dictMapper;


    @Override
    public List<DictVO> selectListByType(Integer typeId) {
        //根据typeId查询省份数据列表
        List<Dict> dictList = dictMapper.selectListByType(typeId);


        //遍历省份数据列表List<Dict>转换为
        List<DictVO> dictVOList = dictList.stream().map(dict -> {
            DictVO dictVO = new DictVO();


            //将dict数据赋值给dictVO
            BeanUtils.copyProperties(dict,dictVO);

            //根据当前数据字典Id查询级联数据字典列表
            List<Dict> subList = dictMapper.selectSubListByPid(dict.getId());

            dictVO.setSubList(subList);

            return dictVO;
        }).collect(Collectors.toList());


    //返回数据
        return dictVOList;
    }
}
